package cn.xhy.material.controller.admin.commodityinfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.xhy.material.controller.admin.commodityinfo.vo.CommodityInfoPageReqVO;
import cn.xhy.material.controller.admin.commodityinfo.vo.CommodityInfoRespVO;
import cn.xhy.material.controller.admin.commodityinfo.vo.CommodityInfoSaveReqVO;
import cn.xhy.material.dal.dataobject.commodityinfo.CommodityInfoDO;
import cn.xhy.material.service.commodityinfo.CommodityInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 商品信息")
@RestController
@RequestMapping("/material/commodity-info")
@Validated
public class CommodityInfoController {

    @Resource
    private CommodityInfoService commodityInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建商品信息")
    @PreAuthorize("@ss.hasPermission('material:commodity-info:create')")
    public CommonResult<Long> createCommodityInfo(@Valid @RequestBody CommodityInfoSaveReqVO createReqVO) {
        return success(commodityInfoService.createCommodityInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品信息")
    @PreAuthorize("@ss.hasPermission('material:commodity-info:update')")
    public CommonResult<Boolean> updateCommodityInfo(@Valid @RequestBody CommodityInfoSaveReqVO updateReqVO) {
        commodityInfoService.updateCommodityInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('material:commodity-info:delete')")
    public CommonResult<Boolean> deleteCommodityInfo(@RequestParam("id") Long id) {
        commodityInfoService.deleteCommodityInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('material:commodity-info:query')")
    public CommonResult<CommodityInfoRespVO> getCommodityInfo(@RequestParam("id") Long id) {
        CommodityInfoDO commodityInfo = commodityInfoService.getCommodityInfo(id);
        return success(BeanUtils.toBean(commodityInfo, CommodityInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品信息分页")
    @PreAuthorize("@ss.hasPermission('material:commodity-info:query')")
    public CommonResult<PageResult<CommodityInfoRespVO>> getCommodityInfoPage(@Valid CommodityInfoPageReqVO pageReqVO) {
        PageResult<CommodityInfoRespVO> pageResult = commodityInfoService.getCommodityInfoPageList(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品信息 Excel")
    @PreAuthorize("@ss.hasPermission('material:commodity-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCommodityInfoExcel(@Valid CommodityInfoPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CommodityInfoDO> list = commodityInfoService.getCommodityInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品信息.xls", "数据", CommodityInfoRespVO.class,
                BeanUtils.toBean(list, CommodityInfoRespVO.class));
    }

}
